


// 给 Date 对象加个方法
if(!Date.prototype.pattern){
    /**
     * 对Date的扩展，将 Date 转化为指定格式的String
     * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符
     * 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
     * eg:
     * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
     * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
     * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
     * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
     * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
     */
    Date.prototype.pattern = function(fmt) {
        let o = {
            "M+" : this.getMonth()+1, //月份
            "d+" : this.getDate(), //日
            "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
            "H+" : this.getHours(), //小时
            "m+" : this.getMinutes(), //分
            "s+" : this.getSeconds(), //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S" : this.getMilliseconds() //毫秒
        };
        let week = {
            "0" : "/u65e5",
            "1" : "/u4e00",
            "2" : "/u4e8c",
            "3" : "/u4e09",
            "4" : "/u56db",
            "5" : "/u4e94",
            "6" : "/u516d"
        };
        if(/(y+)/.test(fmt)){
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        if(/(E+)/.test(fmt)){
            fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);
        }
        for(let k in o){
            if(new RegExp("("+ k +")").test(fmt)){
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            }
        }
        return fmt;
    }
}

export default {

    /**
     * 将 2019-11-25 00:00:00 或者 2019 -11-25  这样的格式转换成 Date 对象
     * @param str
     * @return {Date}
     */
    strTimeToDate(str) {

        let reg = /([1-9][0-9]{3})\s*-\s*(\d{1,2})\s*-\s*(\d{1,2})\s*(\d\d)\s*:\s*(\d\d)\s*:\s*(\d\d)/;
        let ret  = str.match(reg);
        if(ret && ret.length >= 7){

            let year = Number(ret[1]);
            let month = Number(ret[2]) - 1;
            let day = Number(ret[3]);

            let hour = Number(ret[4]);
            let minitue = Number(ret[5]);
            let sec = Number(ret[6]);

            let d = new Date();
            d.setFullYear(year);
            d.setMonth(month);
            d.setDate(day);
            d.setHours(hour);
            d.setMinutes(minitue);
            d.setSeconds(sec);
            return d;

        }else{

            // 匹配只包含日期的
            // 2019- 11- 25
            let reg2 = /([1-9][0-9]{3})\s*-\s*(\d{1,2})\s*-\s*(\d{1,2})/;
            let ret2 = str.match(reg2);
            if(ret2 && ret2.length >= 4){
                let year = Number(ret2[1]);
                let month = Number(ret2[2]) - 1;
                let day = Number(ret2[3]);
                let d = new Date();
                d.setFullYear(year);
                d.setMonth(month);
                d.setDate(day);
                d.setHours(0);
                d.setMinutes(0);
                d.setSeconds(0);
                return d;
            }else{
                throw new Error(`str:${str} can not change to Date`);
            }
        }
    },


    /**
     *
     * @param ms_time {number  | undefined}
     * @return {string}
     */
    getDayStamp(ms_time = undefined){
        if(ms_time === undefined){
            ms_time = Date.now();
        }
        let date =  new Date(ms_time);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        let d = date.getDate();
        return `${y}-${m}-${d}`;
    },

    /**
     * 返回 今天开始的时间
     * 如:需要获得今天 22：30 的时间
     * getTodayTimeMs(22,30);
     * @param hour
     * @param min
     * @return {number} ms
     */
    getTodayTimeMs(hour = 0,min = 0){

        let curTimeMs = G.timeService.getGameTimeMs();
        let date = new Date(curTimeMs);

        let hs = date.getHours();
        let ms = date.getMinutes();
        let ss = date.getSeconds();
        let mss = date.getMilliseconds();

        let dayStart = curTimeMs - hs * 3600 * 1000 - ms * 60 * 1000 - ss * 1000 - mss;
        return dayStart + hour * 3600 * 1000 + min * 60 * 1000;
    },

    /**
     * 获得今天 00:00 的时候的 时间
     * @param timeMs
     * @return {number} ms
     */
    getDayBegin(timeMs){
        let date = new Date(timeMs);
        let hs = date.getHours();
        let ms = date.getMinutes();
        let ss = date.getSeconds();
        let mss = date.getMilliseconds();
        let dayStart = timeMs - hs * 3600 * 1000 - ms * 60 * 1000 - ss * 1000 - mss;
        return dayStart;
    },


    /**
     * @param ms 毫秒
     * @returns {string} 转换成 00:00 形式. 超过1小时  则显示为 00:00:00
     */
    msToStrTime (ms) {
        if (ms < 3600 * 1000) {
            let ret = [];
            let allSec = Math.floor(ms / 1000);
            let min = Math.floor(allSec / 60);
            let sec = allSec % 60;
            if (min < 10) {
                ret.push('0');
                ret.push(min);
            } else {
                ret.push(min);
            }
            ret.push(':');
            if (sec < 10) {
                ret.push('0');
                ret.push(sec);
            } else {
                ret.push(sec);
            }
            return ret.join('');
        } else {
            let ret = [];
            let allSec = Math.floor(ms / 1000);

            let hour = Math.floor(allSec / 3600);
            if (hour < 10) {
                ret.push('0');
                ret.push(hour);
            } else {
                ret.push(hour);
            }
            ret.push(':');

            allSec -= hour * 3600;
            let min = Math.floor(allSec / 60);
            let sec = allSec % 60;
            if (min < 10) {
                ret.push('0');
                ret.push(min);
            } else {
                ret.push(min);
            }
            ret.push(':');
            if (sec < 10) {
                ret.push('0');
                ret.push(sec);
            } else {
                ret.push(sec);
            }
            return ret.join('');
        }
    },

};





